﻿using System;

using UnityEngine;
using UnityEngine.UI;

public class UGUIEnableObject : MonoBehaviour
{
    [SerializeField]
    public GameObject enableObj;

    [SerializeField]
    public float delay;

    [SerializeField]
    public bool resetAfterDisable = true;

    [SerializeField]
    public bool ignoreTimeScale = true;

    private float _time;
    private bool _enabled;

    void OnEnable()
    {
        _time = ignoreTimeScale ? Time.unscaledTime : Time.time;
    }

    void OnDisable()
    {
        if (resetAfterDisable)
        {
            _enabled = false;
            if (enableObj != null && enableObj.activeSelf)
            {
                enableObj.SetActive(false);
            }
        }
    }

    void Update()
    {
        if (_enabled)
        {
            return;
        }

        bool canEnable;

        if (ignoreTimeScale)
        {
            canEnable = (Time.unscaledTime - _time > delay);
        }
        else
        {
            canEnable = (Time.time - _time > delay);
        }

        if (canEnable)
        {
            _enabled = true;
            if (enableObj != null && !enableObj.activeSelf)
            {
                enableObj.SetActive(true);
            }
        }
    }
}